package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.UserInfoMapper;
import org.example.model.user.UserInfo;
import org.example.service.UserInfoService;
import org.example.utils.UserIdThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper,UserInfo> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 添加用户个人信息
     *
     * @param userInfo
     */
    @Override
    public Boolean InsertUserInfo(UserInfo userInfo) {
        if (userInfo == null) { return false; }
        return userInfoMapper.insert(userInfo) > 0;
    }

    /**
     * 根据用户id获取用户个人信息
     */
    @Override
    public UserInfo SelectUserInfoByUserId() {
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return new UserInfo();
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getUserId,userId);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if (userInfo == null) return new UserInfo();
        return userInfo;
    }

    /**
     * 根据id修改用户个人信息
     *
     * @param userInfo
     */
    @Override
    public Boolean UpdateUserInfoById(UserInfo userInfo) {
        if (userInfo == null) return false;
        return userInfoMapper.updateById(userInfo) > 0;
    }
}
